package com.reds.backend.service.impl.article;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reds.backend.pojo.Address;
import com.reds.backend.pojo.Article;
import com.reds.backend.pojo.Homearticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reds.backend.service.article.articleCRUDService;
import com.reds.backend.mapper.articleMapper;
import com.reds.backend.mapper.homearticleMapper;
import java.util.Date;
import java.util.List;
import com.reds.backend.service.check.checkCRUDSerivce;
import com.reds.backend.mapper.addressMapper;

@Service
public class articleCRUDServiceImpl implements articleCRUDService{
    @Autowired
    articleMapper articleMapper;
    @Autowired
    homearticleMapper homearticleMapper;
    @Autowired
    checkCRUDSerivce checkCRUDSerivce;
    @Autowired
    addressMapper addressMapper;

    @Override
    public String addCheck(Article article,String address) {
        checkCRUDSerivce.addArticle(article,address);
        return "success";
    }


    @Override
    public int addNew(Article article) {
        // 查询当前最大 ID
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(id) as id");
        Article maxArticle = articleMapper.selectOne(queryWrapper);

        int newId = 1;
        if (maxArticle != null) {
            newId = maxArticle.getId() + 1;
        }

        // 设置新 ID
        article.setId(newId);

        // 插入
        articleMapper.insert(article);

        // 返回新 ID
        return newId;
    }

    @Override
    public String delete(int id) {
        articleMapper.deleteById(id);
        QueryWrapper<Address> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("aid",id);
        addressMapper.delete(queryWrapper1);
        QueryWrapper<Homearticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",id);
        Homearticle homearticle = new Homearticle();
        homearticle.setAid(Integer.parseInt("1"));
        homearticleMapper.updateById(homearticle);
        return "success";
    }

    @Override
    public String deleteByTime(String time) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("time",time);
        Article article = articleMapper.selectOne(queryWrapper);
        System.out.println(article);
        articleMapper.delete(queryWrapper);
        QueryWrapper<Homearticle> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("aid",article.getId());
        Homearticle homearticle = new Homearticle();
        homearticle.setAid(Integer.parseInt("1"));
        homearticleMapper.updateById(homearticle);
        return null;
    }

    @Override
    public String update(Article article) {
        Date date = new Date();
        article.setTime(date);
        articleMapper.updateById(article);
        return "success";
    }

    @Override
    public List<Article> search(String keyword, int page, int sizes) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        queryWrapper
                .select(Article.class, field -> !field.getColumn().equals("content"))
                .isNull("state");

        return articleMapper.selectList(queryWrapper);
    }
    @Override
    public String setQuickAccess(int id) {
        // 创建更新条件：id匹配
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        // 设置 state 字段为 "快速访问"
        updateWrapper.set("state", "快速访问");

        // 执行更新
        articleMapper.update(null, updateWrapper);
        return "success";
    }

    @Override
    public List<Article> getQuickAccessList() {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        // 查询条件：state 等于 "快速访问"
        queryWrapper.eq("state", "快速访问");

        // 优化：通常列表展示不需要 content 大字段，可以排除以提高性能（参考你原有的search写法）
        queryWrapper.select(Article.class, field -> !field.getColumn().equals("content"));

        // 按时间倒序排列（可选）
        queryWrapper.orderByDesc("time");

        return articleMapper.selectList(queryWrapper);
    }

    @Override
    public String removeQuickAccess(int id) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        // 将 state 设置为 null
        updateWrapper.set("state", null);

        articleMapper.update(null, updateWrapper);
        return "success";
    }
}
